package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.cqut.pojo.vo.noticeInfoVO;
import com.cqut.service.INoticeManagerService;
import com.cqut.dto.CompetitionOnwerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.UserDTO;
import com.cqut.model.NoticeManager;
import com.cqut.model.User;
import com.cqut.util.JSONUtil;
import com.cqut.util.ResponseUtil;
import com.cqut.dto.NoticeManagerDTO;


@RestController
@RequestMapping("/api/noticeManager")
public class NoticeManagerController {
    @Resource(name="noticeManagerService")
    private INoticeManagerService noticeManagerService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> noticeManagerList = noticeManagerService.loadMaps(null, null, null, null, limitShowDTO.getCurPage(), limitShowDTO.getLimit());
        long count = noticeManagerService.count();

        result.put("rows", noticeManagerList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    /**
     * 初始化赛事动态列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/noticeList", method = RequestMethod.GET)
    public ResponseEntity<String> noticeList(
    	NoticeManagerDTO noticeManagerDTO, BindingResult uErrors,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        List<noticeInfoVO>  result = noticeManagerService.loadNoticeList(noticeManagerDTO, limitShowDTO);

        return ResponseUtil.getResEntityForGet(JSONUtil.toJSONArrayForJavaObject(result).toJSONString());
    }
    
    /**
     * 初始化赛事动态列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/noticeListForStates", method = RequestMethod.GET)
    public ResponseEntity<String> noticeListForStates(
    	NoticeManagerDTO noticeManagerDTO, BindingResult uErrors,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        List<noticeInfoVO>  result = noticeManagerService.loadNoticeListStates(noticeManagerDTO, limitShowDTO);
        System.out.println(result);
        return ResponseUtil.getResEntityForGet(JSONUtil.toJSONArrayForJavaObject(result).toJSONString());
    }
    
    /**
     * 初始化当前用户关注赛事的动态列表
     * @param limitShowDTO
     * @param errors
     * @return
     */
    @RequestMapping(value = "/noticeForStatesUser/{userId}", method = RequestMethod.GET)
    public ResponseEntity<String> noticeForStatesUser(
    	@PathVariable("userId") String userId,
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        List<Map<String, Object>>  result = noticeManagerService.loadNoticeStatesUser(userId, limitShowDTO);
        return ResponseUtil.getResEntityForGet(JSONUtil.toJSONArrayForJavaObject(result).toJSONString());
    }
    
    
    /**
     * 根据当前用户ID获取赛事动态列表数量
     */
    @RequestMapping(value="/countStatesUser/{userId}", method=RequestMethod.GET)
	public ResponseEntity<String> countForStateUser(
			@PathVariable("userId") String userId) {
		JSONObject json = new JSONObject();
		json.put("totalItems", noticeManagerService.countForNoticesStatesUser(userId));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
    
    
    /**
     * 根据搜索条件进行筛选列表数量
     */
    @RequestMapping(value="/count", method=RequestMethod.GET)
	public ResponseEntity<String> findModulesWithLimit(
			NoticeManagerDTO noticeManagerDTO, BindingResult uErrors) {
    	if (uErrors.hasErrors())
			ResponseUtil.getResponseEntityWhenInvalidReqParams();
		JSONObject json = new JSONObject();
		json.put("totalItems", noticeManagerService.findCountOfNotices(noticeManagerDTO));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}
    
    /**
     * 前台页面展示赛事动态筛选列表数量
     */
    @RequestMapping(value="/countStates", method=RequestMethod.GET)
	public ResponseEntity<String> findStatesWithLimit(
			NoticeManagerDTO noticeManagerDTO, BindingResult uErrors) {
    	if (uErrors.hasErrors())
			ResponseUtil.getResponseEntityWhenInvalidReqParams();
		JSONObject json = new JSONObject();
		json.put("totalItems", noticeManagerService.findCountOfNoticesStates(noticeManagerDTO));
		
		return ResponseUtil.getResEntityForGet(json.toJSONString());
	}

    @RequestMapping(value = "/{noticeId}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("noticeId") String noticeId) {
        Map<String, Object> noticeManagerModel = noticeManagerService.findMap(noticeId);

        return ResponseUtil.getResEntityForGetAndJson(noticeManagerModel);
    }

    @RequestMapping(value = "noticeAdd", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        NoticeManagerDTO noticeManagerDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();
        NoticeManager noticeManagerModel = noticeManagerDTO.toModel();

        return ResponseUtil.getResEntityForPPP(noticeManagerService.save(noticeManagerModel));
    }

    @RequestMapping(value = "/{noticeId}", method = RequestMethod.PUT)
    public ResponseEntity<String> update(@PathVariable("noticeId") String noticeId,
        @Validated
        NoticeManagerDTO noticeManagerDTO, BindingResult errors) {

        NoticeManager noticeManagerModel = noticeManagerDTO.toModel();
        noticeManagerModel.setNoticeId(noticeId);
        return ResponseUtil.getResEntityForPPP(noticeManagerService.update(noticeManagerModel));
    }

    @RequestMapping(value = "/{noticeId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("noticeId") String noticeId) {
        return ResponseUtil.getResEntityForDel(noticeManagerService.delete(noticeId));
    }

    @RequestMapping(value = "/noticeDel", method = RequestMethod.POST)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
        return ResponseUtil.getResEntityForGet(noticeManagerService.batchDelete(primaryKeys));
    }
    
    @RequestMapping(value = "/detail/{noticeId}", method = RequestMethod.GET)
    public ResponseEntity<String> noticeDetail(@PathVariable("noticeId") String noticeId,
        @Validated
        NoticeManagerDTO noticeManagerDTO, BindingResult errors) {

    	Map<String, Object>  result = noticeManagerService.getNoticeById(noticeId);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    @RequestMapping(value = "/comAndOnwerDetail/{noticeId}", method = RequestMethod.GET)
    public ResponseEntity<String> comAndOnwerDetail(@PathVariable("noticeId") String noticeId,
        @Validated
        CompetitionOnwerDTO competitionOnwerDTO, BindingResult errors) {

    	Map<String, Object>  result = noticeManagerService.getOnwerById(noticeId);
        return ResponseUtil.getResEntityForGetAndJson(result);
    }
    
    
    @RequestMapping(value = "noticeUpdate", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        NoticeManagerDTO noticeManagerDTO, BindingResult errors) {
    	NoticeManager noticeModel = noticeManagerDTO.toModel();
    	noticeModel.setNoticeId(noticeManagerDTO.noticeId);
        return ResponseUtil.getResEntityForPPP(noticeManagerService.update(noticeModel));
    }
    
    /**
	 * 得到大赛管理员下的大赛
	 * */
	@RequestMapping(value = "/competitionList/{userId}", method=RequestMethod.GET)
	public ResponseEntity<String> getCompetition(
			@PathVariable("userId") String userId
			) {
		//这里以后可能要根据登录人的信息来判断，现在先
		List<Map<String, Object>> competitions = noticeManagerService.getCompetition(userId);
		
		return ResponseUtil.getResEntityForGetAndJsonArray(competitions);
	}
}
